/**
 * @Author: Nick Zhang <thesadboy@vip.qq.com>
 * @Description: 虚拟文件监听处理，主要处理文件创建
 * @Date: 2024-06-18 10:22:36
 * @LastEditors: Nick Zhang <thesadboy@vip.qq.com>
 * @LastEditTime: 2024-07-03 11:23:07
 */
package com.nickzhang.jetbrains.file_description_plugin.listeners

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.nickzhang.jetbrains.file_description_plugin.configs.AppSettingState
import com.nickzhang.jetbrains.file_description_plugin.utils.CommentUtil
import com.nickzhang.jetbrains.file_description_plugin.utils.CommonUtil

class AppBulkFileListener : BulkFileListener {
    override fun after(events: MutableList<out VFileEvent>) {
        for (event in events) {
            if (event is VFileCreateEvent && !event.isFromRefresh && !event.file?.isDirectory!!) {
                val file = event.file ?: return
                val state: AppSettingState = AppSettingState.getInstance()
                if (!state.createFileAdd || CommonUtil.checkIsFileIsIgnored(file)) return
                val document = FileDocumentManager.getInstance().getDocument(file)
                ApplicationManager.getApplication().invokeLater {
                    document?.let { CommentUtil.checkAddCommentHeader(it, file) }
                }
            }
        }
    }
}
